package cn.rht.study.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static cn.rht.study.util.OssProperies.ACCESS_KEY_ID;
import static cn.rht.study.util.OssProperies.ACCESS_KEY_SECRET;

public class SmsUtil {

    protected static Logger log = LoggerFactory.getLogger(SmsUtil.class);
    public static SendSmsResponse sendSms(String phone, String code, String codeType){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setSysRegionId("cn-hangzhou");
        request.setPhoneNumbers(phone);
        request.setSignName("猪猪飞侠");
        request.setTemplateCode(codeType);
        request.setTemplateParam("{\"code\":\"" + code + "\"}");
        try {
            SendSmsResponse response = client.getAcsResponse(request);
            return response;
        }catch (ClientException e) {
            log.error("短信发送失败");
        }
        return null;
    }

}
